package com.zhitong.money.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhitong.money.entity.PSalaryParam;
import com.zhitong.money.entity.Position;
import com.zhitong.money.exception.AddException;
import com.zhitong.money.exception.DeleteException;
import com.zhitong.money.exception.RepeatException;
import com.zhitong.money.exception.UpdateException;
import com.zhitong.money.mapper.EmployeeMapper;
import com.zhitong.money.mapper.PSalaryParamMapper;
import com.zhitong.money.mapper.PositionMapper;
import com.zhitong.money.services.api.IEmployeeService;
import com.zhitong.money.services.api.IPositionService;
import org.apache.ibatis.jdbc.Null;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@Slf4j
@Service
public class PositionService implements IPositionService {

    Logger logger= LoggerFactory.getLogger(PositionService.class);

    @Autowired
    private PositionMapper positionMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private PSalaryParamMapper paramMapper;

    /**
     * 通过部门id查询该部门下的所有职位
     * @param did
     * @return
     * @throws Exception
     */
    @Override
    public List<Position> findPositionByDid(String did) throws Exception {
        Map<String,Object> map=new HashMap<>();
        map.put("did",did);
        try {
            List<Position> allByMap = positionMapper.findAllByMap(map);
            if (allByMap==null || allByMap.isEmpty() ){
                throw new NullPointerException("根据部门id查询职位为空");
            }
            return allByMap;
        } catch (Exception e) {
            logger.error("通过did查询职位失败:"+e.getMessage());
            throw e;
        }
    }

    /**
    * @description 根据部门id获取该部门下的所有职位,以及还没有部门的职位
    * @author: Andy Zhang
    * @date: 2021/1/23 16:39
    * @param: did
    * @return:
    */
    @Override
    public List<Position> findNonPosition(String did) throws Exception {

        try {
            Map<String,Object> map=new HashMap<>();
            map.put("did",did);
            List<Position> allByMap = positionMapper.findAllByMap(map);
            map.put("did","00");
            List<Position> allByMap1 = positionMapper.findAllByMap(map);
            allByMap.addAll(allByMap1);
            if (allByMap==null || allByMap.isEmpty()){
                throw new NullPointerException("根据部门id查询该部门下的职位和暂无部门的职位位空");
            }
            return allByMap;
        } catch (Exception e) {
            logger.error("通过did查询职位失败:"+e.getMessage());
            throw e;
        }
    }

    /**
     * 查询所有职位
     * @return
     * @throws Exception
     */
    @Override
    public List<Position> findAllPosition() throws Exception {
        List<Position> allPosition = positionMapper.findAllPosition();
        if (allPosition==null || allPosition.isEmpty()){
            logger.info("查询为空");
            throw new NullPointerException("暂无职位");
        }
        return allPosition;
    }

    /**
     * 分页查询职位
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo findPositionByPage(Map<String,Object> map) throws Exception {
        int pageNum=map.containsKey("pageNum")?(int)map.get("pageNum"):1;
        int pageSize=map.containsKey("pageSize")?(int)map.get("pageSize"):10;
        PageHelper.startPage(pageNum,pageSize);
        List<Position> allPosition = positionMapper.findAllByMap(map);
        if (allPosition==null || allPosition.isEmpty()){
            logger.info("查询为空");
            throw new NullPointerException("暂无职位");
        }
        PageInfo<Position> pageInfo = new PageInfo<>(allPosition);
        return pageInfo;
    }

    /**
     *@Author: AndyZhang
     *@Description 根据map里面的条件查询职位
     *@param map
     *@return
     *@Date: 12:20 2021/1/26
     */
    @Override
    public List<Position> findPositionByMap(Map<String, Object> map) throws Exception {
        try {
            List<Position> allByMap = positionMapper.findAllByMap(map);
            if (allByMap==null || allByMap.isEmpty()){
                throw new NullPointerException("根据map查询职位为空");
            }
            return allByMap;
        } catch (Exception e) {
            logger.error("find position by map error:"+e.getMessage());
            throw e;
        }
    }

    @Override
    public List<Position> findPositionByNoInIds(List<String> ids) throws Exception {
        return positionMapper.fidPositionsByNoInIds(ids);
    }

    /**
     * 通过id查询职位
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public Position findPositionById(String id) throws Exception {
        Position position = positionMapper.findPositionById(id);
        if(position==null){
            logger.info("查询为空");
            throw new NullPointerException("暂无该职位");
        }
        return position;
    }

    /**
     * 添加职位
     * @param position
     * @throws Exception
     */
    @Transactional
    @Override
    public void addPosition(Position position) throws Exception {
        Position position1 = positionMapper.findPositionByName(position.getName().trim());
        if (position1!=null){
            logger.info("职位重复");
            throw new RepeatException("职位重复");
        }
        try {
            positionMapper.addPosition(position);
        } catch (Exception e) {
            logger.error("add position error:"+e.getMessage());
            throw new AddException();
        }

    }

    /**
     * 更新职位
     * @param position
     * @throws Exception
     */
    @Transactional
    @Override
    public void updatePosition(Position position) throws Exception {
        Position position1 = positionMapper.findPositionByName(position.getName().trim());
        if (position1 !=null && !position.getId().equals(position1.getId())){
            throw new RepeatException("职位名称重复");
        }
        try {
            positionMapper.updatePosition(position);
        } catch (Exception e) {
            logger.error("update position error:"+e.getMessage());
            throw new UpdateException();
        }
    }


    /**
     * 删除职位
     * @param id
     * @throws Exception
     */
    @Transactional
    @Override
    public int delPositionById(String id) throws Exception {
        try {
//            表示拥有下属的职位
            List<String> positonByLeaderId = positionMapper.findPositionByLeaderId(id);
            if(positonByLeaderId!=null && !positonByLeaderId.isEmpty()){
//                先把下属的上级id制成00
//                Map<String,Object> map=new HashMap<>();
//                map.put("ids",positonByLeaderId);
//                map.put("leaderId","00");
//                positionMapper.updatePositionByIds(map);
                return -1;
            }
            //把拥有该职位的员工的职位设置成00
            List<String> idsByPid = employeeMapper.findIdsByPid(id);
            if (idsByPid != null && !idsByPid.isEmpty()){
//                employeeMapper.updatePidByIds(idsByPid,"00");
                return -1;
            }

            //查询该职位下的工资参数(有则删除,无则跳过)
            PSalaryParam pSalaryParamByPid = paramMapper.findPSalaryParamByPid(id);
            if (pSalaryParamByPid!=null){
                paramMapper.delPSalaryParam(pSalaryParamByPid.getId());
            }

            int i = positionMapper.delPositionById(id);
            return i;
        } catch (Exception e) {
            logger.error("delete position error:"+e.getMessage());
            throw  new DeleteException();
        }
    }

    /**
     *@Author: AndyZhang
     *@Description 根据多个id同时删除多个职位
     *@param
     *@return
     *@Date: 12:21 2021/1/26
     */
    @Transactional
    @Override
    public int delPositionsByIds(List<String> ids) throws Exception {
        try {
            //把下属的上级id设为00
            List<String> pids = positionMapper.fidPidsByLids(ids);
            if(pids!=null && !pids.isEmpty()){
            //先把下属的上级id制成00
                return -1;
            }

            //把拥有该职位的员工的职位设置成00
            List<String> idsByPids = employeeMapper.findIdsByPids(ids);
            if (idsByPids != null && !idsByPids.isEmpty()){
              return -1;
            }

            //删除该职位下的工资参数
            paramMapper.delPSalaryByPids(ids);

            int i = positionMapper.delPositionSByIds(ids);
            return i;
        } catch (Exception e) {
            logger.error("delPositionsByIds:"+e.getMessage());
            throw new DeleteException();
        }
    }
}
